package com.inossem.warehousestorage_inossem.adapter.emergencyWarehousing;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.emergency_warehousing.EmergenWarehousingReceiveOverviewActivity;
import com.inossem.warehousestorage_inossem.activity.emergency_warehousing.EmergencyWarehousingListActivity;
import com.inossem.warehousestorage_inossem.activity.emergency_warehousing.extra.EmergencyWarehousingExtra;
import com.inossem.warehousestorage_inossem.bean.emergencyWarehousing.EmergencyWarehousingReceiveListBean;

import java.util.List;

/**
 * 作者：wsy on 2017/11/28 10:31
 * <p>
 * 邮箱：445979770@qq.com
 */

public class ReceiveEmergencyWarehousingListAdapter extends BaseAdapter {

    private Context mContext;
    private List<EmergencyWarehousingReceiveListBean.EmergencyWarehousingListBean> mReceiveEmergencyWarehousingList;
    private LayoutInflater inflater;
    private EmergencyWarehousingListActivity.EmergencyWarehousingReceiveListener mEmergencyWarehousingReceiveListener;
    private String mEmergencyWarehousingNumber;

    public ReceiveEmergencyWarehousingListAdapter(Context context, List<EmergencyWarehousingReceiveListBean.EmergencyWarehousingListBean> receiveEmergencyWarehousingList, String emergencyWarehousing, EmergencyWarehousingListActivity.EmergencyWarehousingReceiveListener emergencyWarehousingReceiveListener) {
        mContext = context;
        mReceiveEmergencyWarehousingList = receiveEmergencyWarehousingList;
        mEmergencyWarehousingReceiveListener = emergencyWarehousingReceiveListener;
        inflater = LayoutInflater.from(context);
        mEmergencyWarehousingNumber = emergencyWarehousing;
    }

    @Override
    public int getCount() {
        return mReceiveEmergencyWarehousingList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mReceiveEmergencyWarehousingList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_emerhency_warehousing_receive_list_first_item, null);
            EditText emergencyWarehousingNumber = (EditText) convertView.findViewById(R.id.emergencyWarehousingNumber);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            emergencyWarehousingNumber.setText(mEmergencyWarehousingNumber == null ? "" : mEmergencyWarehousingNumber);
            emergencyWarehousingNumber.setHint(R.string.activity_emergency_warehousing_list_please_enter_the_document_number);
//            inStorageNumber.addTextChangedListener(new QRCodeEditTextWatcher(inStorageNumber) {
//                @Override
//                public void onScan(String text) {
//                    Utils.closeSoftKeyboard(mContext, inStorageNumber);
//                    mInStorageReceiveListener.onNoDoubleClickListener(text);
//                }
//            });
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    mEmergencyWarehousingReceiveListener.onNoDoubleClickListener(emergencyWarehousingNumber.getText().toString());
                }
            });
        } else {
            EmergencyWarehousingReceiveListBean.EmergencyWarehousingListBean receiveEmergencyWarehousingItem = mReceiveEmergencyWarehousingList.get(position - 1);
            ReceiveEmergencyWarehousingListAdapter.ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_emerhency_warehousing_receive_list, null);
                viewHolder = new ReceiveEmergencyWarehousingListAdapter.ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ReceiveEmergencyWarehousingListAdapter.ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, receiveEmergencyWarehousingItem);
        }
        return convertView;
    }

    private void initView(ReceiveEmergencyWarehousingListAdapter.ViewHolder viewHolder, View view) {
        viewHolder.emergencyType = (TextView) view.findViewById(R.id.emergencyType);
        viewHolder.receiptNumber = (TextView) view.findViewById(R.id.receiptNumber);
    }

    private void initData(Context context, ReceiveEmergencyWarehousingListAdapter.ViewHolder viewHolder, View view, EmergencyWarehousingReceiveListBean.EmergencyWarehousingListBean receiveEmergencyWarehousingItem) {
        viewHolder.emergencyType.setText(receiveEmergencyWarehousingItem.getReceipt_type_name());
        viewHolder.receiptNumber.setText(receiveEmergencyWarehousingItem.getUrgence_code());
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent(context, EmergenWarehousingReceiveOverviewActivity.class);
                intent.putExtra(EmergencyWarehousingExtra.RECEIP_NUMBER, receiveEmergencyWarehousingItem.getUrgence_id());
                context.startActivity(intent);
            }
        });
    }

    private class ViewHolder {
        private TextView emergencyType, receiptNumber;
    }
}
